<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<table id="adminuser-datagrid" class="easyui-datagrid"
       data-options="toolbar: '#adminuser-datagrid-toolbar',url: '/adminuser.html?act=list',singleSelect: true,pagination: true,">
    <thead>
    <tr>
        <th data-options="field:'id',width:100">ID</th>
        <th data-options="field:'email',width:200">用户名</th>
        <th data-options="field:'realName',width:130">真实姓名</th>
        <th data-options="field:'nickname',width:130">昵称</th>
        <th data-options="field:'status',width:130,formatter:formatStatus">状态</th>
    </tr>
    </thead>
</table>
<div id="adminuser-datagrid-toolbar" style="height:auto">
    <div style="padding:2px 5px;">
        id: <input class="easyui-textbox" id="search-adminuser-id"/>
        用户名: <input class="easyui-textbox" id="search-adminuser-email">
        真实姓名：<input class="easyui-textbox" id="search-adminuser-realName">
        状态：
        <select style="width:90px" id="search-adminuser-status">
            <option value="">状态</option>
            <option value="1">有效</option>
            <option value="0">无效</option>
        </select>
        <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="doAdminUserSearch()">搜索</a>
    </div>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'"
       onclick="doAdminUserEdit(0)">添加</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'"
       onclick="doAdminUserEdit(1)">编辑</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit'"
       onclick="doAdminUserAssign()">分配角色</a>
</div>
<div id="edit-admin-dialog" class="easyui-dialog" modal="true" title="编辑角色" closed="true"
     href="<%=request.getContextPath()%>/admin_user_edit.jsp" style="width:300px;height:300px;padding-top:30px"
     buttons="#dlg-adminuser-buttons">
</div>
<div id="dlg-adminuser-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitAdminUserForm()">提交</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#edit-adminuser-form').form('reset')">重填</a>
</div>

<div id="user-role-dialog" class="easyui-dialog" modal="true" title="分配角色" style="width: 300px;"
     closed="true" data-options="buttons: '#dlg-user-role-buttons'">
    <ul id="user-role-list" class="easyui-tree"></ul>
</div>
<div id="dlg-user-role-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:assignRole()">确定</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#roleDialog').dialog('close')">取消</a>
</div>
<script type="text/javascript">
    function formatStatus(val, row) {
        if (val == 0) {
            return '<span style="color:#ff0200;">无效</span>';
        } else {
            return '<span style="color:#00cf4d;">有效</span>';
        }
    }

    function doAdminUserSearch() {
        var queryParams = $('#role-datagrid').datagrid('options').queryParams;
        queryParams.id = $("#search-adminuser-id").val();
        queryParams.name = $("#search-adminuser-name").val();
        queryParams.realName = $("#search-adminuser-realName").val();
        $('#adminuser-datagrid').datagrid('reload');
    }

    function doAdminUserEdit(type) {
        if (type == 0) {
            $("#edit-admin-dialog").dialog("open");
            $('#edit-adminuser-form').form('clear');
            $("#edit-adminuser-email").textbox('textbox').attr('readonly', false);
        } else {
            var row = $('#adminuser-datagrid').datagrid('getSelected');
            if (row == null) {
                alert("请选择要编辑的数据");
                return;
            }
            $("#edit-admin-dialog").dialog({
                onLoad: function () {
                    $("#edit-adminuser-email").textbox('textbox').attr('readonly', true);
                    $('#edit-adminuser-form').form('load', {
                        name: row.name,
                        id: row.id,
                        realName: row.realName,
                        status: row.status,
                        email: row.email,
                        nickname: row.nickname
                    });
                }
            });
            $("#edit-admin-dialog").dialog("open");
        }
    }

    function submitAdminUserForm() {
        $('#edit-adminuser-form').form('submit', {
            onSubmit: function () {
                if ($(this).form('enableValidation').form('validate')) {
                    $.ajax({
                        url: "/adminuser.html?act=edit",
                        data: $('#edit-adminuser-form').serialize(),
                        method: "post",
                        success: function (data) {
                            if (data.status) {

                                $("#edit-admin-dialog").dialog("close");
                                $('#adminuser-datagrid').datagrid('reload');
                            } else {
                                alert(data.message);
                            }
                        }
                    })
                }
                return false;
            }
        });
    }

    var roleTree;

    function doAdminUserAssign() {
        var row = $('#adminuser-datagrid').datagrid('getSelected');
        if (row != null && row != "") {
            //加载所有菜单
            var resultData = new Array();
            $.ajax({
                url: '/adminuser.html?act=user_role',
                type: 'POST',
                data: {userId: row.id},
                dataType: "json",
                cache: false,
                success: function (json) {
                    resultData = json;
                    //加载菜单树列表
                    roleTree = $("#user-role-list").tree({
                        url: '/adminuser.html?act=role_list',
                        method: "post",
                        checkbox: true,
                        formatter: function (node) {
                            var s = node.name +"["+node.remark+"]";
                            return s;
                        },
                        onLoadSuccess: function (node, data) {
                            //点击选中用户已有角色
                            for (var i = 0; i < resultData.length; i++) {
                                roleTree.tree("check", roleTree.tree("find", resultData[i]).target);
                            }
                            $("#user-role-dialog").dialog("open");
                        }
                    });
                }
            });
        }
    }

    function assignRole() {
        var row = $('#adminuser-datagrid').datagrid('getSelected');
        var roleIds = new Array();
        $(roleTree.tree("getChecked")).each(function (i, node) {
            roleIds[i] = node["id"];
        });
        $.ajax({
            url: '/adminuser.html?act=assign_role',
            type: 'POST',
            data: {roleIds: roleIds.join(","), userId: row["id"]},
            cache: false,
            success: function (data) {
                if (data.status) {
                    $("#user-role-dialog").dialog("close");
                } else {
                    alert(data.message);
                }
            }
        });
    }
</script>
